#!/bin/bash

cd ~

# 安装Python依赖
sudo pip3 install websocket-client==0.48.0 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip3 install requests -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip3 install pyzbar -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip3 install pyserial -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip3 install opencv-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip3 install grpcio -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip3 install grpcio-tools -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

if [ ! -d "/usr/local/bin/geekros" ]; then
  sudo touch /etc/systemd/system/geekros.service
  sudo sh -c 'echo "[Unit]" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "Description=Geek Ros Service" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "After=network.target" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "[Service]" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "ExecStart=/usr/local/bin/geekros start" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "[Install]" >> /etc/systemd/system/geekros.service'
  sudo sh -c 'echo "WantedBy=multi-user.target" >> /etc/systemd/system/geekros.service'
  sudo systemctl enable geekros.service
fi

# 下载、解压Geek Ros
sudo wget https://cdn.robomentor.cn/mentor/client/Geek_Ros-0.0.1.tar.gz && sudo tar -C ./ -xzf Geek_Ros-*.tar.gz && sudo rm -rf Geek_Ros-*.tar.gz && sudo mv Geek_Ros-* Geek_Ros

# 编译
cd Geek_Ros && export GO111MODULE=on && export GOPROXY=https://goproxy.io

sudo -E /usr/local/go/bin/go build main.go && sudo mv main /usr/local/bin/geekros && cd ~ && sudo rm -rf Geek_Ros && sudo rm -rf go && sudo systemctl restart geekros.service

geekros version
